package net.dreamlu.api.life;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import net.dreamlu.util.HttpUtil;
import net.dreamlu.util.OathConfig;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 生活帮助类api
 * @author L.cm
 * @date Jul 10, 2013 10:26:26 PM
 */
public class HelpApi {
	
	private static final String WEATHER_URL = "http://api.map.baidu.com/telematics/v2/weather";
	private static final String TRANSLATE_URL = "http://openapi.baidu.com/public/2.0/bmt/translate";
	private static final String IP_URL = "http://api.map.baidu.com/location/ip";
	
	public static final String DEFAULT_CITY = "北京"; // 默认城市
	
	/**
	 * 百度天气api
	 * @param @param params
	 * @param @return    设定文件
	 * @return JSONObject    返回类型
	 * @throws
	 * <url>http://developer.baidu.com/map/carapi-7.htm</url>
	 */
	public static JSONArray weather(String data){
		// location=北京&output=json&ak=6c19c46381dd73b0b5f6a5c0c8acde42
		Map<String, String> params = Maps.newHashMap();
		params.put("location", data);
		params.put("ak", OathConfig.getValue("lbs_ak_baidu"));
		params.put("output", "json");
		JSONObject json = JSONObject.fromObject(HttpUtil.doGet(WEATHER_URL, params));
		if (null != json && 0 == json.getInt("error")) {
			return json.getJSONArray("results");
		} else {
			return null;
		}
	}
	
	/**
	 * 百度翻译api
	 * @param @param data
	 * @param @return    设定文件
	 * @return JSONObject    返回类型
	 * @throws
	 * <url>http://developer.baidu.com/wiki/index.php?title=%E5%B8%AE%E5%8A%A9%E6%96%87%E6%A1%A3%E9%A6%96%E9%A1%B5/%E7%99%BE%E5%BA%A6%E7%BF%BB%E8%AF%91/%E7%BF%BB%E8%AF%91API</url>
	 */
	public static JSONArray translate(String data){
		// client_id=xwKOgtKjbbrn9dOb7ZkGrAo5&q=today&from=auto&to=auto
		Map<String, String> params = Maps.newHashMap();
		params.put("q", data);
		params.put("client_id", OathConfig.getValue("openid_baidu"));
		params.put("from", "auto");
		params.put("to", "auto");
		JSONObject json = JSONObject.fromObject(HttpUtil.doGet(TRANSLATE_URL, params));
		if (null != json && null == json.get("trans_result")) {
			return json.getJSONArray("trans_result");
		} else {
			return null;
		}
	}
	
	/**
	 * 百度地理位置 ip
	 * @param @param ip
	 * @param @return    设定文件
	 * @return JSONObject    返回类型
	 * @throws
	 * <url>http://developer.baidu.com/map/ip-location-api.htm</url>
	 */
	public static JSONObject ipInfo(String ip){
		Map<String, String> params = Maps.newHashMap();
		params.put("ip", ip);
		params.put("ak", OathConfig.getValue("lbs_ak_baidu"));
		JSONObject json = JSONObject.fromObject(HttpUtil.doGet(IP_URL, params));
		if (null != json && 0 == json.getInt("status")) {
			return json.getJSONObject("content");
		} else {
			return null;
		}
	}
}
